<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="退货编号" prop="returnOrderNo">
        <el-input v-model="queryParams.returnOrderNo" placeholder="请输入退货编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="退货名称" prop="returnName">
        <el-input v-model="queryParams.returnName" placeholder="请输入退货名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户id" prop="clientId">
        <el-input v-model="queryParams.clientId" placeholder="请输入客户id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="[' ReturnInfo:ReturnInfo:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="[' ReturnInfo:ReturnInfo:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="[' ReturnInfo:ReturnInfo:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="[' ReturnInfo:ReturnInfo:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ReturnInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="退货编号" align="center" prop="returnOrderNo" width="150" />
      <el-table-column label="退货名称" align="center" prop="returnName" />
      <el-table-column label="客户" align="center" prop="client.clientName" />
      <el-table-column label="退货原因" align="center" prop="returnContent" >
        <template slot-scope="scope">
          <span v-if="scope.row.returnContent == 1">
            <el-tag type="danger">损坏</el-tag>
          </span>
          <span v-if="scope.row.returnContent == 2">
            <el-tag type="primary">其他</el-tag>
          </span>


        </template>
        </el-table-column>
      <el-table-column label="退货数量" align="center" prop="clientNum" />
      <el-table-column label="退款金额" align="center" prop="marketPrice" />
      <el-table-column label="单位" align="center" prop="marketUnit" />
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.auditStatus == 0">
            <el-tag type="danger">未审核</el-tag>
          </span>
          <span v-if="scope.row.auditStatus == 1">
            <el-tag type="primary">审核</el-tag>
          </span>
          <span v-if="scope.row.auditStatus == 2">
            <el-tag type="success">已审批</el-tag>
          </span>

        </template>
      </el-table-column>
      <el-table-column label="审批意见" align="center" prop="approverOpinion" />
      <el-table-column label="审批时间" align="center" prop="approverTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.approverTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审批人" align="center" prop="approverPerson" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <span v-if="scope.row.auditStatus == 0">
            <el-button size="mini" type="text" icon="el-icon-success" @click="handleApprover(scope.row)"
              v-hasPermi="['ReturnInfo:ReturnInfo:approver']">审批</el-button>
          </span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

       <!-- 审批供应商对话框 -->
    <el-dialog :title="title1" :visible.sync="openApprover" width="960x" append-to-body>
      <el-form ref="form" :model="formApprover" :rules="rules" label-width="180px">
        <el-form-item label="审批状态" prop="auditStatus">
          <el-radio-group v-model="formApprover.auditStatus">
            <el-radio label="1">不通过</el-radio>
            <el-radio label="2">通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审批意见" prop="approverOpinion">
          <el-input v-model="formApprover.approverOpinion" type="textarea" placeholder="请输入审核意见" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormApprover">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



    <!-- 添加或修改退货单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="退货编号" prop="returnOrderNo">
          <el-input v-model="form.returnOrderNo" placeholder="请输入退货编号" />
        </el-form-item>
        <el-form-item label="退货名称" prop="returnName">
          <el-input v-model="form.returnName" placeholder="请输入退货名称" />
        </el-form-item>
        <el-form-item label="客户id" prop="clientId">
          <el-input v-model="form.clientId" placeholder="请输入客户id" />
        </el-form-item>
        <el-form-item label="退货数量" prop="clientNum">
          <el-input v-model="form.clientNum" placeholder="请输入退货数量" />
        </el-form-item>
        <el-form-item label="退款金额" prop="marketPrice">
          <el-input v-model="form.marketPrice" placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="单位" prop="marketUnit">
          <el-input v-model="form.marketUnit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { approverReturn,listReturnInfo, getReturnInfo, delReturnInfo, addReturnInfo, updateReturnInfo } from "@/api/ReturnInfo/ReturnInfo";

export default {
  name: "ReturnInfo",
  data() {
    return {
      openApprover: false,
      formApprover: {
        financeId: 0
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 退货单表格数据
      ReturnInfoList: [],
      // 弹出层标题
      title: "",
      title1:"",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        returnOrderNo: null,
        returnName: null,
        clientId: null,
        clientNum: null,
        marketPrice: null,
        marketUnit: null,
        auditStatus: null,
        approverOpinion: null,
        approverTime: null,
        approverPerson: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    submitFormApprover(){
      approverReturn(this.formApprover).then(response => {
        this.$modal.msgSuccess("审批操作成功");
        this.openApprover = false;
        this.getList();

      })
    },
    handleApprover(row){
      this.formApprover.returnId = row.returnId;
      this.openApprover = true;
      this.title1 = "审批窗口";
    },
    /** 查询退货单列表 */
    getList() {
      this.loading = true;
      listReturnInfo(this.queryParams).then(response => {
        this.ReturnInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        returnId: null,
        returnOrderNo: null,
        returnName: null,
        clientId: null,
        clientNum: null,
        marketPrice: null,
        marketUnit: null,
        auditStatus: null,
        approverOpinion: null,
        approverTime: null,
        approverPerson: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.returnId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加退货单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const returnId = row.returnId || this.ids
      getReturnInfo(returnId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改退货单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.returnId != null) {
            updateReturnInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReturnInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const returnIds = row.returnId || this.ids;
      this.$modal.confirm('是否确认删除退货单编号为"' + returnIds + '"的数据项？').then(function () {
        return delReturnInfo(returnIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(' ReturnInfo/ReturnInfo/export', {
        ...this.queryParams
      }, `ReturnInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
